package cn.cjxy.day15employeemanagement.controller;

import cn.cjxy.day15employeemanagement.domain.Dept;
import cn.cjxy.day15employeemanagement.service.DeptService;
import cn.cjxy.day15employeemanagement.utils.PageRequest;
import cn.cjxy.day15employeemanagement.utils.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangjie
 * @date 2025/10/21
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 新增
     * @param dept
     * @return
     */
    @PostMapping
    public R addDept(@RequestBody Dept dept){
        deptService.addDept(dept);
        return R.ok();
    }

    /**
     * 修改部门信息
     * @param dept
     * @return
     */
    @PutMapping
    public R updateDept(@RequestBody Dept dept){
        deptService.updateDept(dept);
        return R.ok();
    }

    /**\
     * 删除部门信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteDept(@PathVariable("id") Long id){
        deptService.delDeptById(id);
        return R.ok();
    }

     /**
     * 根据id查询部门信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Dept> selectById(@PathVariable("id") Long id){
        Dept dept = deptService.selectById(id);
        return R.ok(dept);
    }

  /*
     * 查询所有
     * @return
    */
    @GetMapping
    public R<PageInfo<Dept>> selectAll(@RequestBody PageRequest pageRequest){
        PageInfo<Dept> deptPageInfo = deptService.selectAllByPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        return R.ok(deptPageInfo);
    }

    //todo  分页查询 批量添加 批量删除
     /**
     * 批量添加部门
     * @param depts
     * @return
     */
    @PostMapping("/batch")
    public R<Void> batchAddDepts(@RequestBody List<Dept> deptList){
        try {
            deptService.batchAddDepts(deptList);
            return R.ok("批量添加成功");
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }

    }
}
